package com.cms.framework.web;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.validation.Validator;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.context.request.WebRequest;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class WebBindingInitializer extends ConfigurableWebBindingInitializer {
    private List<Validator> validators;

    public void initBinder(WebDataBinder webDataBinder, WebRequest webRequest) {
        super.initBinder(webDataBinder, webRequest);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        webDataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); // true:允许输入空值，false:不能为空值
        // 枚举处理器
//        webDataBinder.registerCustomEditor(PayType.class, new EnumEditor<PayType>(PayType.class, true));
//        webDataBinder.registerCustomEditor(MemberTypeEnum.class, new EnumEditor<MemberTypeEnum>(MemberTypeEnum.class, true));
//        webDataBinder.registerCustomEditor(MemberCertifiedStatusEnum.class, new EnumEditor<MemberCertifiedStatusEnum>(MemberCertifiedStatusEnum.class, true));
//        webDataBinder.registerCustomEditor(MemberFreezeStatusEnum.class, new EnumEditor<MemberFreezeStatusEnum>(MemberFreezeStatusEnum.class, true));
//        webDataBinder.registerCustomEditor(MemberGenderEnum.class, new EnumEditor<MemberGenderEnum>(MemberGenderEnum.class, true));
//        webDataBinder.registerCustomEditor(MemberIsactiveEnum.class, new EnumEditor<MemberIsactiveEnum>(MemberIsactiveEnum.class, true));
//        webDataBinder.registerCustomEditor(MemberLoginStatusEnum.class, new EnumEditor<MemberLoginStatusEnum>(MemberLoginStatusEnum.class, true));
//        webDataBinder.registerCustomEditor(MemberSourceEnum.class, new EnumEditor<MemberSourceEnum>(MemberSourceEnum.class, true));
    }

    public List<Validator> getValidators() {
        return validators;
    }

    public void setValidators(List<Validator> validators) {
        this.validators = validators;
    }

}
